﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using RMS.Controller;

namespace RMS.View
{
    public partial class ResearchProjectView : UserControl
    {
        private ResearchProjectController _controller;

        public ResearchProjectController Controller
        {
            get { return this._controller; }
            set { this._controller = value; }
        }

        public ResearchProjectView()
        {
            InitializeComponent();
        }

        public string TextBoxProjectId { get { return this.textBoxProjectId.Text; } }
        public string TextBoxProjectName { get { return this.textBoxProjectName.Text; } }
        public string TextBoxProjectDescription { get { return this.textBoxTeamDescription.Text; } }
        public string TextBoxTeamId { get { return this.textBoxTeamId.Text; } }
        public string TextBoxTeamName { get { return this.textBoxTeamName.Text; } }
        public string TextBoxTeamDescription { get { return this.textBoxTeamDescription.Text; } }
        public string TextBoxPhaseId { get { return this.textBoxPhaseId.Text; } }
        public string TextBoxPhaseName { get { return this.textBoxPhaseName.Text; } }
        public string TextBoxPhaseDescription { get { return this.textBoxPhaseDescription.Text; } }
        public string TextBoxActivityId { get { return this.textBoxActivityId.Text; } }
        public string TextBoxActivityName { get { return this.textBoxActivityName.Text; } }
        public string TextBoxActivityDescription { get { return this.textBoxActivityDescription.Text; } }
        public string TextBoxActivityPriority { get { return this.textBoxActivityPriority.Text; } }
        public string TextBoxExpenseId { get { return this.textBoxExpenseId.Text; } }
        public string ComboBoxExpenseType { get { return this.comboBoxExpenseType.Text; } }
        public string TextBoxExpenseValue { get { return this.textBoxExpenseValue.Text; } }
        public string TextBoxHours { get { return this.textBoxHours.Text; } }


        private void buttonAddProject_Click(object sender, EventArgs e)
        {
            this._controller.HandleAddProjectButtonClick();
        }

        private void buttonRemoveProject_Click(object sender, EventArgs e)
        {
            //if (this.listViewProjects.SelectedItems.Count > 0)
            this._controller.HandleRemoveProjectButtonClick();
        }

        private void buttonUpdateProject_Click(object sender, EventArgs e)
        {
            this._controller.HandleUpdateProjectButtonClick();
        }

        private void buttonSearchProject_Click(object sender, EventArgs e)
        {
            if (this.buttonSearchProject.Text == "Search")
            {
                this.buttonSearchProject.Text = "Cancel";
                this._controller.HandleSearchProjectButtonClick();
            }
            else if (this.buttonSearchProject.Text == "Cancel")
            {
                this.buttonSearchProject.Text = "Search";
                this._controller.HandleCancelSearchProjectButtonClick();
            }
        }

        private void buttonRemoveMember_Click(object sender, EventArgs e)
        {
            if (this.listViewTeamMembers.SelectedItems.Count > 0)
            {
                ListViewItem item = new ListViewItem();
                ListViewItem selectedItem = this.listViewTeamMembers.SelectedItems[0];
                item.Text = selectedItem.Text;
                item.SubItems.Add(selectedItem.SubItems[1]);
                item.SubItems.Add(selectedItem.SubItems[2]);
                item.SubItems.Add(selectedItem.SubItems[3]);
                item.SubItems.Add(selectedItem.SubItems[4]);
                item.SubItems.Add(selectedItem.SubItems[5]);
                item.SubItems.Add(selectedItem.SubItems[6]);
                item.SubItems.Add(selectedItem.SubItems[7]);
                item.SubItems.Add(selectedItem.SubItems[8]);
                this.listViewTeamMembers.SelectedItems[0].Remove();
                this.listViewProspectMembers.Items.Add(item);
            }
        }

        private void buttonAddMember_Click(object sender, EventArgs e)
        {
            if (this.listViewProspectMembers.SelectedItems.Count > 0)
            {
                ListViewItem item = new ListViewItem();
                ListViewItem selectedItem = this.listViewProspectMembers.SelectedItems[0];
                item.Text = selectedItem.Text;
                item.SubItems.Add(selectedItem.SubItems[1]);
                item.SubItems.Add(selectedItem.SubItems[2]);
                item.SubItems.Add(selectedItem.SubItems[3]);
                item.SubItems.Add(selectedItem.SubItems[4]);
                item.SubItems.Add(selectedItem.SubItems[5]);
                item.SubItems.Add(selectedItem.SubItems[6]);
                item.SubItems.Add(selectedItem.SubItems[7]);
                item.SubItems.Add(selectedItem.SubItems[8]);
                this.listViewProspectMembers.SelectedItems[0].Remove();
                this.listViewTeamMembers.Items.Add(item);
            }
        }

        public void PopulateProjectList(List<Model.ResearchProject> list)
        {
            this.listViewProjects.Items.Clear();
            foreach (Model.ResearchProject project in list)
            {
                ListViewItem item = new ListViewItem();
                item.Text = project.Name;
                item.SubItems.Add(project.Id.ToString());
                item.SubItems.Add(project.Name);
                item.SubItems.Add(project.Description);
                item.SubItems.Add(project.Team.Id.ToString());
                item.SubItems.Add(project.Team.Name);
                item.SubItems.Add(project.Team.Description);
                this.listViewProjects.Items.Add(item);
            }
        }

        private void listViewProjects_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listViewProjects.SelectedItems.Count <= 0) return;

            this.textBoxPhaseId.Text = "";
            this.textBoxPhaseName.Text = "";
            this.textBoxPhaseDescription.Text = "";
            this.textBoxHours.Text = "";
            this.textBoxActivityId.Text = "";
            this.textBoxActivityName.Text = "";
            this.textBoxActivityDescription.Text = "";
            this.textBoxActivityPriority.Text = "";
            this.textBoxExpenseId.Text = "";
            this.textBoxExpenseValue.Text = "";
           
            ListViewItem item = listViewProjects.SelectedItems[0];
            this.textBoxProjectId.Text = item.SubItems[1].Text;
            this.textBoxProjectName.Text = item.SubItems[2].Text;
            this.textBoxProjectDescription.Text = item.SubItems[3].Text;
            this.textBoxTeamId.Text = item.SubItems[4].Text;
            this.textBoxTeamName.Text = item.SubItems[5].Text;
            this.textBoxTeamDescription.Text = item.SubItems[6].Text;
            this._controller.RefreshTeamMembersList();
            this._controller.RefreshPhaseList();
        }

        private void listViewTeamMembers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listViewTeamMembers.SelectedItems.Count <= 0) return;
            this.textBoxHours.Text = listViewTeamMembers.SelectedItems[0].SubItems[3].Text;
        }

        private void listViewProspectMembers_SelectedIndexChanged(object sender, EventArgs e)
        {
            // nothing ^_^
        }

        private void listViewPhases_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listViewPhases.SelectedItems.Count <= 0) return;
            this.textBoxHours.Text = "";
            this.textBoxActivityId.Text = "";
            this.textBoxActivityName.Text = "";
            this.textBoxActivityDescription.Text = "";
            this.textBoxActivityPriority.Text = "";
            this.textBoxExpenseId.Text = "";
            this.textBoxExpenseValue.Text = "";
           
            ListViewItem item = this.listViewPhases.SelectedItems[0];
            this.textBoxPhaseId.Text = item.SubItems[1].Text;
            this.textBoxPhaseName.Text = item.SubItems[2].Text;
            this.textBoxPhaseDescription.Text = item.SubItems[3].Text;
            this._controller.RefreshActivitiesList();
        }

        private void listViewActivities_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listViewActivities.SelectedItems.Count <= 0) return;

            this.textBoxHours.Text = "";
            this.textBoxExpenseId.Text = "";
            this.textBoxExpenseValue.Text = "";
           
            ListViewItem item = this.listViewActivities.SelectedItems[0];
            this.textBoxActivityId.Text = item.SubItems[1].Text;
            this.textBoxActivityName.Text = item.SubItems[2].Text;
            this.textBoxActivityDescription.Text = item.SubItems[3].Text;
            this.textBoxActivityPriority.Text = item.SubItems[4].Text;
            this._controller.RefreshExpensesList();
        }

        private void listViewExpenses_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listViewExpenses.SelectedItems.Count <= 0) return;
            ListViewItem item = this.listViewExpenses.SelectedItems[0];
            this.textBoxExpenseId.Text = item.SubItems[1].Text;
            this.comboBoxExpenseType.Text = item.SubItems[2].Text;
            this.textBoxExpenseValue.Text = item.SubItems[3].Text;
        }

        private void buttonAddPhase_Click(object sender, EventArgs e)
        {
            this._controller.HandleAddPhaseButtonClick();
        }

        private void buttonRemovePhase_Click(object sender, EventArgs e)
        {
            if(this.listViewPhases.SelectedItems.Count > 0)
                this._controller.HandleRemovePhaseButtonClick();
        }

        private void buttonUpdatePhase_Click(object sender, EventArgs e)
        {
            this._controller.HandleUpdatePhaseButtonClick();
        }

        private void buttonSearchPhase_Click(object sender, EventArgs e)
        {
            if (this.buttonSearchPhase.Text == "Search")
            {
                this.buttonSearchPhase.Text = "Cancel";
                this._controller.HandleSearchPhaseButtonClick();
            }
            else if (this.buttonSearchPhase.Text == "Cancel")
            {
                this.buttonSearchPhase.Text = "Search";
                this._controller.HandleCancelSearchPhaseButtonClick();
            }
        }

        private void buttonAddActivity_Click(object sender, EventArgs e)
        {
            this._controller.HandleAddActivityButtonClick();
        }

        private void buttonRemoveActivity_Click(object sender, EventArgs e)
        {
            if(this.listViewActivities.SelectedItems.Count > 0)
                this._controller.HandleRemoveActivityButtonClick();
        }

        private void buttonUpdateActivity_Click(object sender, EventArgs e)
        {
            this._controller.HandleUpdateActivityButtonClick();
        }

        private void buttonSearchActivity_Click(object sender, EventArgs e)
        {
            if (this.buttonSearchActivity.Text == "Search")
            {
                this.buttonSearchActivity.Text = "Cancel";
                this._controller.HandleSearchActivityButtonClick();
            }
            else if (this.buttonSearchActivity.Text == "Cancel")
            {
                this.buttonSearchActivity.Text = "Search";
                this._controller.HandleCancelSearchActivityButtonClick();
            }
        }

        private void buttonAddExpense_Click(object sender, EventArgs e)
        {
            this._controller.HandleAddExpenseButtonClick();
        }

        private void buttonRemoveExpense_Click(object sender, EventArgs e)
        {
            if(this.listViewExpenses.SelectedItems.Count > 0)
                this._controller.HandleRemoveExpenseButtonClick();
        }

        private void buttonUpdateExpense_Click(object sender, EventArgs e)
        {
            this._controller.HandleUpdateExpenseButtonClick();
        }

        private void buttonSearchExpense_Click(object sender, EventArgs e)
        {
            if (this.buttonSearchExpense.Text == "Search")
            {
                this.buttonSearchExpense.Text = "Cancel";
                this._controller.HandleSearchExpenseButtonClick();
            }
            else if (this.buttonSearchExpense.Text == "Cancel")
            {
                this.buttonSearchExpense.Text = "Search";
                this._controller.HandleCancelSearchExpenseButtonClick();
            }
        }

        private void buttonUpdateHours_Click(object sender, EventArgs e)
        {
            if (this.listViewTeamMembers.SelectedItems.Count > 0)
                this._controller.HandleHoursButtonClick(Convert.ToInt32(this.listViewTeamMembers.SelectedItems[0].SubItems[1].Text));
        }

        public void PopulateTeamMemberList(List<Model.ResearchTeamMember> list)
        {
            this.listViewTeamMembers.Items.Clear();
            foreach (Model.ResearchTeamMember member in list)
            {
                ListViewItem item = new ListViewItem();
                item.Text = member.Name;
                item.SubItems.Add(member.Id.ToString());
                item.SubItems.Add(member.Name);
                item.SubItems.Add(member.HoursCount.ToString());
                this.listViewTeamMembers.Items.Add(item);
            }
        }

        public void PopulatePhaseList(List<Model.ResearchPhase> list)
        {
            this.listViewPhases.Items.Clear();
            foreach (Model.ResearchPhase phase in list)
            {
                ListViewItem item = new ListViewItem();
                item.Text = phase.Name;
                item.SubItems.Add(phase.Id.ToString());
                item.SubItems.Add(phase.Name);
                item.SubItems.Add(phase.Description);
                this.listViewPhases.Items.Add(item);
            }
        }

        public void PopulateActivitiesList(SortedList<int, Model.ResearchActivity> sortedList)
        {
            this.listViewActivities.Items.Clear();
            foreach (Model.ResearchActivity activity in sortedList.Values)
            {
                ListViewItem item = new ListViewItem();
                item.Text = activity.Name;
                item.SubItems.Add(activity.Id.ToString());
                item.SubItems.Add(activity.Name);
                item.SubItems.Add(activity.Description);
                item.SubItems.Add(activity.Order.ToString());
                this.listViewActivities.Items.Add(item);
            }
        }

        public void PopulateExpensesList(List<Model.Expense> list)
        {
            this.listViewExpenses.Items.Clear();
            foreach (Model.Expense expense in list)
            {
                ListViewItem item = new ListViewItem();
                item.Text = expense.Value.ToString();
                item.SubItems.Add(expense.Id.ToString()); 
                item.SubItems.Add(expense.Type.ToString());
                Console.WriteLine(expense.Type.ToString());
                item.SubItems.Add(expense.Value.ToString());                
                this.listViewExpenses.Items.Add(item);
            }
        }
    }
}
